home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue99 / CPROG9.CPP < prev   
Encoding:
C/C++ Source or Header  |  1994-10-05  |  1.2 KB  |  38 lines

  1. /* CPROG9.CPP - run it, then read the comments. */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. main()
  8. {
  9. int i, x;
  10.  
  11.     randomize();
  12.  
  13.     for(i=0; i < 20; i++)    // We'll do what we're going to do 20 times
  14.       {
  15.       x=random(100);    // Put a random number (range 0-99) in x
  16.       printf("%2d is %s\n", x, ((x % 2 == 1 ) ? "odd" : "even") );
  17.       }
  18. }
  19.         /* The printf() line has three components:
  20.            "%2d is %s\n"  - The 2 between % and d right-aligns the
  21.             number in a field two chaarcters wide. Single-digit
  22.             numbers therefore format neatly. The %s expects
  23.             a string as the third parameter.
  24.             x - The second parameter goes into %2d in the format
  25.             string ('format string' is the name given to the
  26.             first parameter in printf() )
  27.             ( (x % 2 == 1 ) ? "odd" : "even" )
  28.             Evaluates as the string "odd" or the string "even"
  29.             depending on the result of the test (x % 2 == 1)
  30.             % is the modulo operator. It yields the remainder
  31.             of dividing x by 2. Odd numbers will give 1, evens
  32.             will give 0. The outer pair of brackets shouldn't
  33.             be necessary, but the compiler seems to get
  34.             confused if you don't help it work out which bit
  35.             belongs to what.
  36.         */
  37.  
  38.